package com.tianyiyan.tyenword;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.opensymphony.xwork2.ActionSupport;
import com.tianyiyan.dao.tyenword.VocabularyDao;
import com.tianyiyan.hibernate.tyenword.Vocabulary;

public class CheckAnswerAction extends ActionSupport
{
	private Map<String, Object> dataMap = new HashMap<String, Object>();
	
	private String answers;
	
	public String getAnswers() {
		return answers;
	}
	public void setAnswers(String answers) {
		this.answers = answers;
	}
	public Map<String, Object> getDataMap() {
		return dataMap;
	}
	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}
	@Override
	public String execute() throws Exception {
		List<Elem> errors = new ArrayList<Elem>();
		
		if (answers != null)
		{
			List<Elem> answerList = JSON.parseArray(answers, Elem.class);
			VocabularyDao dao = new VocabularyDao();
			errors = dao.filterError(answerList);
			if (errors.size() > 0)
			{
				dataMap.put("state", "error");
				dataMap.put("errors", errors);
				System.out.println(errors);
			}
			else
			{
				dataMap.put("state", "success");
			}
		}
		return SUCCESS;
	}

}
